package com.iaz.tech.tools.uml.file.impl;

import javax.swing.JOptionPane;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.iaz.tech.tools.uml.IUMLNeuronAble;
import com.iaz.tech.tools.uml.file.NeuronJSONTools;
import com.iaz.tech.tools.uml.relationship.impl.NeuronAssociationsRelationship;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@EqualsAndHashCode(callSuper=false)
public class NeuronAssociationsLoader extends NeuronJSONTools{
    /**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(NeuronAssociationsLoader.class);
    
    @Override
    public void parseJSON(IUMLNeuronAble umlObj, JSONObject json) {
        try {
            if (umlObj == null) {
                throw new IllegalArgumentException("umlObj 不能为空");
            }
            if (json == null) {
                throw new IllegalArgumentException("json 不能为空");
            }
            if (!(umlObj instanceof NeuronAssociationsRelationship)){
                throw new IllegalArgumentException("umlObj 必须是 NeuronAssociationsRelationship 类型");
            } 
            NeuronAssociationsRelationship associations = (NeuronAssociationsRelationship) umlObj;
            if (json.containsKey("dialing")) {
                associations.setDialingStr(json.getString("dialing"));
            }
            if (json.containsKey("called")) {
                associations.setCalledStr(json.getString("called"));
            }
            if (json.containsKey("dialingConfig")) {
                associations.setDialingConfig(json.getString("dialingConfig"));
            }
            if (json.containsKey("calledConfig")) {
                associations.setCalledConfig(json.getString("calledConfig"));
            }
            if (json.containsKey("dialingDescr")) {
                associations.setDialingDescr(json.getString("dialingDescr"));
            }
        } catch (Exception e) {
            logger.error("解析关联关系失败", e);
        }        
    }

    @Override
    public JSONObject toJSON(IUMLNeuronAble objStruct) {
        JSONObject result = new JSONObject();
        try {
            if (objStruct == null) {
                throw new IllegalArgumentException("objStruct 不能为空");
            }
            if (!(objStruct instanceof NeuronAssociationsRelationship)){
                throw new IllegalArgumentException("objStruct 必须是 NeuronAssociationsRelationship 类型");
            } 
            NeuronAssociationsRelationship associations = (NeuronAssociationsRelationship) objStruct;
            
            result.put("dialing", associations.getDialingStr());
            result.put("called", associations.getCalledStr());
            result.put("dialingConfig", associations.getDialingConfig());
            result.put("calledConfig", associations.getCalledConfig());
            result.put("dialingDescr", associations.getDialingDescr()); 
        }catch(Exception e) {
            logger.error("转换关联关系失败", e);
           result = new JSONObject();
        }
        return result;
    }
}
